/**
 * Created by csy on 2017/11/20.
 */
var $alert = require("libs/alert");
var isSale = require('libs/user-type').isSale;
require(['jquery','libs/confirm','easyui'],function($,$confirm){
    //工单类型
    appendOption('api/v1/p/mms/ticket/typelist',$("#level1"),"id","name",{id:'',name:'全部'});
    $("#level1").combobox({
        onChange: function (newVal,oldVal) {
            if(newVal){
                appendOption('api/v1/p/mms/ticket/subTypelist?id='+newVal,$("#level2"),"id","name",{id:'',name:'全部'});
            }
        }
    });
    appendOption('api/v1/p/crm/warehouse/query?warehouseType=A',$("#warehouseId"),"warehouseId","warehouseName",{warehouseId:'',warehouseName:'全部'});
    $.parser.parse();
    tableInit();
    //查询按钮点击事件
    $("#main_search").on("click",function () {
        $('#main_table').datagrid('load',queryParams());
    });
    $(document).on("click",'a[name=open]',function () {
        var id = $(this).data('id');
        $confirm("是否打开工单？",function () {
            ajaxRequestMethod({id: id,status:"open"},"api/v1/p/mms/ticket/updateTicket");
        });
    });
    $(document).on("click",'a[name=close]',function () {
        var id = $(this).data('id');
        $confirm("是否关闭工单？",function () {
            ajaxRequestMethod({id: id,status:"close"},"api/v1/p/mms/ticket/updateTicket");
        });
    });
    $(document).on("click",'a[name=look]',function () {
        window.parent.tabOpen($(this).attr('ref'),"工单详情",true,true);
    });
});

function tableInit() {
    $("#main_table").datagrid({
        url:'api/v1/p/mms/ticket/list',
        border : false,
        height:$(window).height(),
        pagination : true,
        idField : 'id',
        pageSize : 20,
        pageList : [ 20, 50,  100 ],
        rownumbers:true,
        singleSelect: true,
        emptyMsg : "暂无数据",
        queryParams:queryParams(),
        columns:[[ {
            field : 'ticketNo',
            title : '工单编号',
            width:"12%",
            formatter: function(value,row,index){
                return "<a name='look' href='javascript:;' ref='ticket-detail?ticketId="+row.id+"'>"+value+"</a>";
            }
        },{
            field : 'title',
            title : '工单内容',
            width:"30%",
            formatter: function(value,row,index){
                return '<span  title='+value+'>'+value+'</span>'
            }
        },{
            field : 'priority',
            title : '优先级',
            width:"10%",
            formatter: function(value,row,index){
                if(value == 1){
                    return '一般';
                }else if(value == 2){
                    if(row.status != 'closed'){
                        return '<span style="color: red">重要</span>';
                    }else{
                        return '重要';
                    }
                }else{
                    return '';
                }
            }
        },{
            field : 'type',
            title : '工单类型',
            width:"8%"
        },{
            field : 'subType',
            title : '工单子类型',
            width:"8%"
        },{
            field:'customerName',
            title:'关联客户',
            width:"8%"
        },{
            field:'warehouseName',
            title:'关联仓库',
            width:"8%"
        },{
            field:'createDate',
            title:'提交时间',
            width:"12%"
        },{
            field:'status',
            title:'状态',
            width:"6%",
            formatter:function (value,row,index) {
                switch (value){
                    case "committed": return "处理中";
                    case "processed": return "已处理";
                    case "closed": return "已完成";
                    default : return "";
                }
            }
        },{
            field:'score',
            title:'客户评分',
            width:"8%",
            formatter:function (value,row,index) {
                switch (value){
                    case 5: return "非常好";
                    case 4: return "好评";
                    case 3: return "一般";
                    case 2: return "差评";
                    case 1: return "非常差";
                    default : return "待评价";
                }
            }
        },{
            field:'opt',
            title:'操作',
            hidden:isSale,
            width:"10%",
            formatter:function (value,row,index) {
                switch (row.status){
                    case "committed": return "<a name='look' href='javascript:;' ref='ticket-detail?ticketId="+row.id+"'>查看</a>|<a href='javascript:;' name='close' data-id='"+row.id+"'>关闭工单</a>";
                    case "processed": return "<a name='look' href='javascript:;' ref='ticket-detail?ticketId="+row.id+"'>查看</a>|<a href='javascript:;' name='open' data-id='"+row.id+"'>开启工单</a>";
                    case "closed": return "<a name='look' href='javascript:;' ref='ticket-detail?ticketId="+row.id+"'>查看</a>";
                    default : return "";
                }
            }
        }
        ]],
        loadFilter:function(data){
            if(data.code=='200'){
                return data.data;
            }else{
                return [];
            }
        },
        toolbar:"#tb"
    });
}
function queryParams() {
    var d1 = $('#startDt').datebox('getValue');
    var d2 = $('#endDt').datebox('getValue');
    var data =
        {
            ticketNo:$('#ticketNo').val(),
            level1:$('#level1').combobox('getValue'),
            level2:$('#level2').combobox('getValue'),
            priority:$('#priority').combobox('getValue'),
            customerName:$('#customerName').val(),
            warehouseId:$('#warehouseId').combobox('getValue'),
            dateFrom:d1?$('#startDt').data('utc0'):'',
            dateTo:d2?$('#endDt').data('utc0'):''
        };
    return data;
}
function ajaxRequestMethod(data,url) {
    $.ajax({
        contentType: "application/json; charset=utf-8",
        data:JSON.stringify(data),
        type:"post",
        url:url,
        success:function (result) {
            if(result.code==200){
                $('#main_table').datagrid('load');
            }else {
                $alert(result.message);
            }
        },
        error:function () {
            console.log(url+" error")
        }
    });
}
//添加参数
function appendOption(url,$select,valueField,textField,all) {
    $select.combobox({
        url:url,
        valueField:valueField,
        textField:textField,
        loadFilter:function(result){
            if(result.code=='200'){
                result.data.unshift(all);
                return result.data;
            }
        }
    })
}